package com.yf.aclient.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import android.util.Log;


/**
 * 日期工具类 用于转换String到Date和Timestamps
 * 
 */
public class DateUtil {
	private static String defaultDatePattern = null;
	private static String timePattern = "HH:mm";
	
	private  static  String tag  =DateUtil.class.toString();

	/**
	 * 根据本地化的资源包<code>date.format</code>的定义， 获得日期默认格式(yyyy-MM-dd)
	 * 
	 * @return UI显示日期的格式
	 */
	public static String getDatePattern() {
//		try {
//			defaultDatePattern = ResourceBundle.getBundle(Constants.BUNDLE_KEY,
//					locale).getString("date.format");
//		} catch (MissingResourceException mse) {
			defaultDatePattern = "yyyy-MM-dd";
//		}

		return defaultDatePattern;
	}

	public static String getDateTimePattern() {
		return DateUtil.getDatePattern() + " HH:mm:ss.S";
	}

	/**
	 * 用于转换Oracle格式的日期转换
	 * 
	 * @param aDate
	 *            数据库格式的日期
	 * @return 格式化为UI显示的日期
	 */
	public static final String getDate(Date aDate) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (aDate != null) {
			df = new SimpleDateFormat(getDatePattern());
			returnValue = df.format(aDate);
		}

		return (returnValue);
	}

	/**
	 * 返回当前时间:yyyy-MM-dd HH:mm
	 */
	public static String getTime(Date theTime) {
		return convertDateToString(theTime, timePattern);
	}

	/**
	 * 返回当前日期，格式为：yyyy-MM-dd
	 */
	public static Calendar getToday() throws ParseException {
		Date today = new Date();
		SimpleDateFormat df = new SimpleDateFormat(getDatePattern());

		String todayAsString = df.format(today);
		Calendar cal = new GregorianCalendar();
		cal.setTime(convertStringToDate(todayAsString));

		return cal;
	}

	/**
	 * 日期转换为字符串
	 */
	public static final String convertDateToString(Date aDate) {
		return convertDateToString(aDate, getDatePattern());
	}

	/**
	 * 根据给定的日期/时间格式，转换日期
	 * 
	 * @param date
	 * @param format
	 *            日期格式:EEEE是星期, MM月,dd日,yyyy是年(ASCII 字符)
	 * @return
	 */
	public static String convertDateToString(Date aDate, String format) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (format == null || format.equals("")) {
			format = getDatePattern();
		}

		if (aDate == null) {
			Log.e(tag,"日期为空!");
			
		} else {
			df = new SimpleDateFormat(format);
			returnValue = df.format(aDate);
		}

		return (returnValue);
	}

	/**
	 * 字符串转换为日期
	 */
	public static Date convertStringToDate(String strDate)
			throws ParseException {
		Date aDate = null;

		try {
			//if (logger.isDebugEnabled()) {
				Log.e(tag,"格式为: " + getDatePattern());
			//}

			aDate = convertStringToDate(getDatePattern(), strDate);
		} catch (ParseException pe) {
			Log.e(tag,"不能把 '" + strDate + "' 转换为日期，抛出异常" + pe);
			throw new ParseException(pe.getMessage(), pe.getErrorOffset());
		}

		return aDate;
	}

	/**
	 * 根据给定的日期格式，转换日期
	 * 
	 * @see java.text.SimpleDateFormat
	 */
	public static final Date convertStringToDate(String format, String strDate)
			throws ParseException {
		SimpleDateFormat df = null;
		Date date = null;
		df = new SimpleDateFormat(format);

			Log.e(tag,"转换日期： '" + strDate + "' 的格式为 '" + format + "'");

		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			Log.e(tag,"ParseException: " + pe);
			throw new ParseException(pe.getMessage(), pe.getErrorOffset());
		}

		return (date);
	}

	/**
	 * 通用日期计算
	 * 
	 * @param date
	 * @param dateType
	 *            计算的是年(Calendar.YEAR)、月(Calendar.MONTH)、日(Calendar.DATE)类型
	 * @param amount
	 * @return
	 */
	public static Date calcDate(Date date, int dateType, int amount) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(dateType, amount);

		return c.getTime();
	}

	/**
	 * 对日进行加减计算
	 * 
	 * @param date
	 *            需要计算的日期
	 * @param amount
	 *            加减的数量
	 * @return 计算后时间
	 */
	public static Date calcDateByDay(Date date, int amount) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, amount);

		return c.getTime();
	}

	/**
	 * 对月进行加减计算
	 * 
	 * @param date
	 *            需要计算的日期
	 * @param amount
	 *            加减的数量
	 * @return 计算后时间
	 */
	public static Date calcDateByMonth(Date date, int amount) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, amount);

		return c.getTime();
	}

	/**
	 * 对年进行加减计算
	 * 
	 * @param date
	 *            需要计算的日期
	 * @param amount
	 *            加减的数量
	 * @return 计算后时间
	 */
	public static Date calcDateByYear(Date date, int amount) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, amount);

		return c.getTime();
	}

	/**
	 * 获得月初时间
	 * 
	 * @param date当前日期
	 * @return 月初时间
	 */
	public static Date getStartMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1);

		return c.getTime();
	}

	/**
	 * 获得月末时间
	 * 
	 * @param date当前日期
	 * @return 月末时间
	 */
	public static Date getEndMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1);
		c.add(Calendar.MONTH, 1);
		c.add(Calendar.DAY_OF_MONTH, -1);

		return c.getTime();
	}

	/**
	 * 获得年初时间
	 * 
	 * @param date当前日期
	 * @return 年初时间
	 */
	public static Date getStartYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(c.get(Calendar.YEAR), Calendar.JANUARY, 1);

		return c.getTime();
	}

	/**
	 * 获得年末时间
	 * 
	 * @param date当前日期
	 * @return 年末时间
	 */
	public static Date getEndYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(c.get(Calendar.YEAR), Calendar.DECEMBER, 31);

		return c.getTime();
	}

	/**
	 * 获得周的第一天，周的第一天为星期天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getStartWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		int f = Calendar.SUNDAY;
		int amount = f - dayOfWeek;

		return calcDateByDay(date, amount);
	}

	/**
	 * 获得周的最后一天，周的最后一天为星期六
	 * 
	 * @param date
	 * @return
	 */
	public static Date getEndWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		int f = Calendar.SATURDAY;
		int amount = f - dayOfWeek;

		return calcDateByDay(date, amount);
	}

	/**
	 * 获得季的第一天，季的第一天为星期天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getStartSeason(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int m = c.get(Calendar.MONTH);
		if (m <= 3) {
			c.set(c.get(Calendar.YEAR), Calendar.JANUARY, 1);
		} else if (m <= 6) {
			c.set(c.get(Calendar.YEAR), Calendar.APRIL, 1);
		} else if (m <= 9) {
			c.set(c.get(Calendar.YEAR), Calendar.JULY, 1);
		} else if (m <= 12) {
			c.set(c.get(Calendar.YEAR), Calendar.OCTOBER, 1);
		}

		return c.getTime();
	}

	/**
	 * 获得季的最后一天，季的最后一天为星期六
	 * 
	 * @param date
	 * @return
	 */
	public static Date getEndSeason(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int m = c.get(Calendar.MONTH) + 1;
		if (m <= 3) {
			c.set(c.get(Calendar.YEAR), Calendar.MARCH, 31);
		} else if (m <= 6) {
			c.set(c.get(Calendar.YEAR), Calendar.JUNE, 30);
		} else if (m <= 9) {
			c.set(c.get(Calendar.YEAR), Calendar.SEPTEMBER, 30);
		} else if (m <= 12) {
			c.set(c.get(Calendar.YEAR), Calendar.DECEMBER, 31);
		}

		return c.getTime();
	}


	/***************************************************************************
	 * 根据日期判断是第几季度
	 * 
	 * @param date
	 * @return
	 */
	public static int getNumberSeason(Date date) {
		int currentMonth = date.getMonth() + 1;
		int currentSeason = currentMonth % 3 == 0 ? currentMonth / 3
				: currentMonth / 3 + 1;
		return currentSeason;
	}

	public int getDay(String strDate) {
		Calendar cal = parseDateTime(strDate);
		return cal.get(Calendar.DATE);
	}

	public int getMonth(String strDate) {
		Calendar cal = parseDateTime(strDate);

		return cal.get(Calendar.MONTH) + 1;
	}

	public int getWeekDay(String strDate) {
		Calendar cal = parseDateTime(strDate);
		return cal.get(Calendar.DAY_OF_WEEK);
	}

	public String getWeekDayName(String strDate) {
		String mName[] = { "日", "一", "二", "三", "四", "五", "六" };
		int iWeek = getWeekDay(strDate);
		iWeek = iWeek - 1;
		return "星期" + mName[iWeek];
	}

	public int getYear(String strDate) {
		Calendar cal = parseDateTime(strDate);
		return cal.get(Calendar.YEAR) + 1900;
	}

	public static Calendar parseDateTime(String baseDate) {
		Calendar cal = null;
		cal = new GregorianCalendar();
		int yy = Integer.parseInt(baseDate.substring(0, 4));
		int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
		int dd = Integer.parseInt(baseDate.substring(8, 10));
		int hh = 0;
		int mi = 0;
		int ss = 0;
		if (baseDate.length() > 12) {
			hh = Integer.parseInt(baseDate.substring(11, 13));
			mi = Integer.parseInt(baseDate.substring(14, 16));
			ss = Integer.parseInt(baseDate.substring(17, 19));
		}
		cal.set(yy, mm, dd, hh, mi, ss);
		return cal;
	}

	public int DateDiff(String strDateBegin, String strDateEnd, int iType) {
		Calendar calBegin = parseDateTime(strDateBegin);
		Calendar calEnd = parseDateTime(strDateEnd);
		long lBegin = calBegin.getTimeInMillis();
		long lEnd = calEnd.getTimeInMillis();
		int ss = (int) ((lBegin - lEnd) / 1000L);
		int min = ss / 60;
		int hour = min / 60;
		int day = hour / 24;
		if (iType == 0)
			return hour;
		if (iType == 1)
			return min;
		if (iType == 2)
			return day;
		else
			return -1;
	}

	/***************************************************************************
	 * @功能 判断某年是否为闰年
	 * @return boolean
	 * @throws ParseException
	 **************************************************************************/
	public boolean isLeapYear(int yearNum) {
		boolean isLeep = false;
		/** 判断是否为闰年，赋值给一标识符flag */
		if ((yearNum % 4 == 0) && (yearNum % 100 != 0)) {
			isLeep = true;
		} else if (yearNum % 400 == 0) {
			isLeep = true;
		} else {
			isLeep = false;
		}
		return isLeep;
	}

	/***************************************************************************
	 * @功能 计算当前日期某年的第几周
	 * @return interger
	 * @throws ParseException
	 **************************************************************************/
	public int getWeekNumOfYear() {
		Calendar calendar = Calendar.getInstance();
		int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
		return iWeekNum;
	}

	/***************************************************************************
	 * @功能 计算指定日期某年的第几周
	 * @return interger
	 * @throws ParseException
	 **************************************************************************/
	public int getWeekNumOfYearDay(String strDate) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date curDate = format.parse(strDate);
		calendar.setTime(curDate);
		int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
		return iWeekNum;
	}

	/***************************************************************************
	 * @功能 计算某年某周的开始日期
	 * @return interger
	 * @throws ParseException
	 **************************************************************************/
	public String getYearWeekFirstDay(int yearNum, int weekNum)
			throws ParseException {

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, yearNum);
		cal.set(Calendar.WEEK_OF_YEAR, weekNum);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		// 分别取得当前日期的年、月、日
		String tempYear = Integer.toString(yearNum);
		String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
		String tempDay = Integer.toString(cal.get(Calendar.DATE));
		String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
		return SetDateFormat(tempDate, "yyyy-MM-dd");

	}

	/***************************************************************************
	 * @功能 计算某年某周的结束日期
	 * @return interger
	 * @throws ParseException
	 **************************************************************************/
	public String getYearWeekEndDay(int yearNum, int weekNum)
			throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, yearNum);
		cal.set(Calendar.WEEK_OF_YEAR, weekNum + 1);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		// 分别取得当前日期的年、月、日
		String tempYear = Integer.toString(yearNum);
		String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
		String tempDay = Integer.toString(cal.get(Calendar.DATE));
		String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
		return SetDateFormat(tempDate, "yyyy-MM-dd");
	}

	/***************************************************************************
	 * @功能 计算某年某月的开始日期
	 * @return interger
	 * @throws ParseException
	 **************************************************************************/
	public String getYearMonthFirstDay(int yearNum, int monthNum)
			throws ParseException {

		// 分别取得当前日期的年、月、日
		String tempYear = Integer.toString(yearNum);
		String tempMonth = Integer.toString(monthNum);
		String tempDay = "1";
		String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
		return SetDateFormat(tempDate, "yyyy-MM-dd");

	}

	/***************************************************************************
	 * @功能 计算某年某月的结束日期
	 * @return interger
	 * @throws ParseException
	 **************************************************************************/
	public String getYearMonthEndDay(int yearNum, int monthNum)
			throws ParseException {

		// 分别取得当前日期的年、月、日
		String tempYear = Integer.toString(yearNum);
		String tempMonth = Integer.toString(monthNum);
		String tempDay = "31";
		if (tempMonth.equals("1") || tempMonth.equals("3")
				|| tempMonth.equals("5") || tempMonth.equals("7")
				|| tempMonth.equals("8") || tempMonth.equals("10")
				|| tempMonth.equals("12")) {
			tempDay = "31";
		}
		if (tempMonth.equals("4") || tempMonth.equals("6")
				|| tempMonth.equals("9") || tempMonth.equals("11")) {
			tempDay = "30";
		}
		if (tempMonth.equals("2")) {
			if (isLeapYear(yearNum)) {
				tempDay = "29";
			} else {
				tempDay = "28";
			}
		}
		// System.out.println("tempDay:" + tempDay);
		String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
		return SetDateFormat(tempDate, "yyyy-MM-dd");
	}

	/**
	 * @see 取得指定时间的给定格式()
	 * @return String
	 * @throws ParseException
	 */
	public String SetDateFormat(String myDate, String strFormat)
			throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
		String sDate = sdf.format(sdf.parse(myDate));

		return sDate;
	}

}
